var ie6 = !-[1,] && !window.XMLHttpRequest; 
//关闭标签页     
function CloseWin(){window.opener=null;window.open("","_self");window.close()};
//字号调节
var $speech=$('.myart'),defaultsize=$speech.css('font-size');
$('#switcher a').click(function(){var num=parseFloat($speech.css('font-size'),10);switch(this.id){case'small':num/=1.4;break;case'big':num*=1.4;break;default:num=parseFloat(defaultsize,10)}$speech.css('font-size',num+'px')});
//返回顶部
$('body').on('click','.gotop',function(){$('html,body').stop().animate({scrollTop:'0'},300)});
//幻灯片
$.fn.slide=function(){
var defaults,opts,data_opts,$this,$b_,t,n=0,count,$nav,$p,$n,DelayObj,Delay=false;
	defaults={
		fade:true,
		auto:true,
		time:4000,
		act:'mouseover',
		fn:null
		};
	$this=$(this);
	data_opts=$this.data('slide')||{};
	opts=$.extend({},defaults,data_opts);
	$b_=$this.children('.ban_c');
	count=$b_.length;
	if($this.find('.ban_nav').length){$nav=$this.find('.ban_nav')}else{
			$nav=$('<div class="ban_nav"></div>');
			for(i=0;i<count;i++){$nav.append('<a>'+(i+1)+'</a>')};
			$this.append($nav)
			};
	$this.append('<a class="Left"></a><a class="Right"></a>');
	$nav.children('a').eq(0).addClass('on');
	$nav.children('a').eq(1).addClass('ban_next');
	$nav.children('a').eq(count-1).addClass('ban_prev');
	$b_.hide().eq(0).show();
	if(ie6){$b_.height($b_.attr('height') || $this.height())};
	if(count && count!=1) {
	$nav.children('a').each(function(index) {
		$(this).on(opts.act,function(event) {
			event.preventDefault();
			event.stopPropagation();
			if (index >= count)return;
			$nav.children('a').eq(index-1).addClass('ban_prev').siblings().removeClass('ban_prev');
			$nav.children('a').eq(index==count-1 ? 0 : index+1).addClass('ban_next').siblings().removeClass('ban_next')
			if(opts.fade){$b_.stop(1,1).fadeOut(500).eq(index).stop(1,1).fadeIn(600)}else{$b_.hide().eq(index).show()};
			$(this).addClass('on').siblings().removeClass("on");
			n=index;
		})
	});
	$p=$this.find('.Left');
	$n=$this.find('.Right');
	if(opts.auto){
		t = setInterval(function(){showAuto()}, opts.time);
		$b_.hover(function(){
			DelayObj=setTimeout(function(){
				clearInterval(t);
				Delay=true;
				},300)
			},function(){
				clearTimeout(DelayObj);
				if(Delay){
					t=setInterval(function(){showAuto()},opts.time);
					showAuto();
					Delay=false
					}
				})
		};
	$p.click(function(){showPre()});
	$n.click(function(){showAuto()});
	function showAuto(){n=n>=(count - 1) ? 0 : ++n;$nav.find('a').eq(n).trigger(opts.act)};
	function showPre(){n=n<=0 ? (count - 1) : --n;$nav.find('a').eq(n).trigger(opts.act)};
	}
	if(opts.fn){eval(opts.fn+"($this,$b_,$nav,$n)")}
};
$('.slide').each(function() {$(this).slide()});
//下拉
$.fn.sele=function() {
	var ev=($(this).data('sele')?$(this).data('sele').act:'mouseenter').split(" ");
	$(this).bind(ev[0],function(){sele_show(this,ev[0])}).bind('mouseleave',function(){sele_hide(this)});
	function sele_show(me,evt){
		$(me).find('.sele_').stop(1,0).slideDown(200).children('a').click(function(event){
			event.stopPropagation();
			if(!$(this).attr('target')){
				event.preventDefault();
				if($(me).find('.btn').length){
					$(me).find('.btn').attr('href',$(this).attr('href'))
				};
				$(this).parent().hide().parents('.sele').find('span').text($(this).text());
			}
		})
	};
	function sele_hide(me){
		$(me).find('.sele_').stop(1,0).slideUp(200)
	};
};
var $sele=$('.sele');
$sele.each(function(i) {$(this).css('z-index',$sele.length-i).sele()});
//ie6定位修复
$.fn.ie6Fix=function(){
	var $this=$(this),
		 posiTop=$this.css('top')!='auto' ? true : ($this.css('bottom')!='auto' ? false : null),
		 selfHei=$(this).outerHeight(true),
		 $H,	
		 posiVal=parseInt($this.css(posiTop ? 'top' : 'bottom'));
	if(posiTop!=null){
		$this.css('position','absolute');
		rePosi();
		$(window).resize(function(){rePosi();});
		$(window).scroll(function(){rePosi();});
		function rePosi(){
			$H=$(window).height();
			$this.css('top',posiTop ? $(window).scrollTop()+posiVal : $H-selfHei-posiVal+$(window).scrollTop());
			};
	}else{
		console.warn('ie6Fix():对象缺少top或bottom属性')
		}
};
if(ie6){$('.ie6Fix').each(function() {$(this).ie6Fix()})};
//搜索框
$('#s').focus(function(){
	if($(this).val()==''){
		$(this).parent('form').attr('class','focous');
		$(this).on('input',function(){
			$(this).parent('form').addClass('input')
		})
	}
	}).blur(function(){
		$(this).parent('form').removeClass('focous');
		if($(this).val()==''){
			$(this).parent('form').removeAttr('class')
			}
		});
	//for ie
if(document.all){
	  var $s=document.getElementById('s');
	  if($s.attachEvent) {
			$s.attachEvent('onpropertychange',function(e) {
				 if(e.propertyName!='value') return;
				 $($s).trigger('input');
			});
	  }
};
//导航
var $nav=$('.nav').find('.li'),
	 $navCur=$('.nav').find('.cur').length?$('.nav').find('.cur').index():0,
	 $navLine=$('.nav_line');
$navLine.css('left',$navCur*$nav.eq(0).width());
$nav.mouseenter(function(){
	$(this).addClass('hover');
	$navLine.stop(1,0).animate({left:$(this).index()*$nav.eq(0).width()},200)
	}).mouseleave(function(){
		$(this).removeClass('hover');
		$navLine.stop(1,0).animate({left:($('.nav').find('.cur').length?$('.nav').find('.cur').index():0)*$nav.eq(0).width()},200)
		});
//banner 
function banner_ext($this,$b_,$nav,$n){
	var bt=setInterval(function(){
		$n.trigger('click');
		},4000),
		key=$this.find('.ban_pause');
	key.click(function(){
			clearInterval(bt);
		if($(this).hasClass('ban_play')){
				$(this).removeClass('ban_play');
				bt=setInterval(function(){
					$n.trigger('click');
					},4000)
			}else{
				$(this).addClass('ban_play');
				}
		})
	};
//单行上下
$.fn.newsUp=function(){
	var $this=$(this)
		,$news=$this.find('ul')
		,$num=$news.find('li').length
		,$hei=$this.height()
		,$pre=$this.find('.pre_')
		,$nex=$this.find('.nex_')
		,$i=0;
	$nex.click(function(){$i<$num-1 ? $i++ : $i=0;ani($i)});
	$pre.click(function(){$i>0 ? $i-- : $i=$num-1;ani($i)});
	function ani($i){$news.animate({top:-$hei*$i+'px'},400)};
	setInterval(function(){$nex.click()},4000)
};
$('.newsUp').each(function() {$(this).newsUp()});
//侧边导航
var sideMenu=$('.side_menu').children('dd');
if(sideMenu.length){
	sideMenu.click(function(event){
		if($(this).find('ul').length){
			$(this).toggleClass('open');
			}
		});
	};
//roll
$.fn.roll=function(){
	var $keylist=$(this).find('a'),
		 $View=$(this).parent().next('.view'),
		 $SildeWidth=$View.width(),
		 allpage=$View.find('.gallery_cell').length,
		 nowpage=0,
		 $now,
		 $all,
		 $haspage=$(this).parent().find('.now').length && $(this).parent().find('.all').length;
	if($haspage){$now=$(this).parent().find('.now');$all=$(this).parent().find('.all');$now.text(nowpage+1);$all.text(allpage)};
	$View.find('.gallery').width($SildeWidth*allpage);
	$View.find('.gallery_cell').width($SildeWidth);
	if($keylist.parent().children('.left').length && $keylist.parent().children('.right').length){
		$keylist.click(function(event){
			event.preventDefault();
			if($(this).hasClass('right')){
				nowpage = nowpage >=(allpage - 1) ? 0 : ++nowpage;roll1(nowpage);if($haspage){$now.text(nowpage+1)}
			}else if($(this).hasClass('left')){
				nowpage = nowpage <=0 ? (allpage - 1) : --nowpage;roll1(nowpage);if($haspage){$now.text(nowpage+1)}
			}
			function roll1(nowpage){$View.children('.gallery').stop(1,0).animate({marginLeft:-$SildeWidth*nowpage+'px'},400)}
			})
		}else{
		   var keycur=0,autotimer=setInterval(function(){keycur = keycur < allpage-1 ? keycur+1 : 0;roll2(keycur)},4000);
			$keylist.eq(0).addClass('cur');
			$View.hover(function(){clearInterval(autotimer)},function(){autotimer=setInterval(function(){keycur = keycur < allpage-1 ? keycur+1 : 0;roll2(keycur)},4000)})
			$keylist.each(function(index) {
            $(this).mouseenter(function(){keycur=index;roll2(keycur)})
         });
			function roll2(keycur){$keylist.eq(keycur).addClass('cur').siblings().removeClass('cur');$View.children('.gallery').stop(1,0).animate({marginLeft:-$SildeWidth*keycur+'px'},400)}
			}
	};
$('.roll').each(function() {$(this).roll()});
//纵向滚动
$.fn.scroll_v=function(opt,callback){
	 if(!opt) var opt={};
	 var _btnUp = $("#"+ opt.up),
	 	 _btnDown = $("#"+ opt.down),
	 	 timerID,
	 	 _this=this.eq(0).find("ul:first"),
	    lineH=_this.find("li").outerHeight(true), 
		 line=opt.line?parseInt(opt.line,10):parseInt(this.height()/lineH,10), 
		 speed=opt.speed?parseInt(opt.speed,10):500,
		 timer=opt.timer || 30000,
		 cols,lasted;
	 line=line?line:1;
	 var upHeight=0-line*lineH;
	 var scrollUp=function(){
		 	cols=Math.floor(_this.width()/_this.find("li").width());
			_btnUp.unbind("click",scrollUp);
			_this.animate({marginTop:upHeight},speed,function(){
					_this.find("li:lt("+cols+")").appendTo(_this);
					_this.css('marginTop',0);
					_btnUp.bind("click",scrollUp);
			})
	 };
	 var scrollDown=function(){
		 lasted=_this.find("li").length-Math.floor(_this.width()/_this.find("li").width())-1;
			_btnDown.unbind("click",scrollDown);
			for(i=1;i<=line;i++){_this.find("li:gt("+lasted+")").show().prependTo(_this)};
			_this.css({marginTop:upHeight});
			_this.animate({marginTop:0},speed,function(){
				_btnDown.bind("click",scrollDown)
			})
	 };
	 var autoPlay = function(){if(timer)timerID = window.setInterval(scrollUp,timer)};
	 var autoStop = function(){if(timer)window.clearInterval(timerID)};
	 _this.hover(autoStop,autoPlay).mouseout();
	 _btnUp.click( scrollUp ).hover(autoStop,autoPlay);
	 _btnDown.click( scrollDown ).hover(autoStop,autoPlay);
  };    
	
//订购表单增删
var orederWrap=$('#order_wrap'),
	 sortOrder=function(){
		 orederWrap.find('.no').each(function(i) {$(this).text(i+1)});
		 },
	 addOrder=function(){
		 if(orederWrap.children('tr').length>=10) return;
		 orederWrap.append('<tr><td class="no"></td><td><input type="text" /></td><td><input type="text" /></td><td><input type="text" /></td><td><input type="text" /></td><td><input type="text" /></td><td><input type="text" /></td><td><a class="del">删除</a></td></tr>');
		 sortOrder();
		 };
orederWrap.on('click','.del',function(){
	 $(this).parents('tr').remove();
	 sortOrder();
	 if(!orederWrap.children('tr').length){
		 addOrder();
		 }
	 }
 );
